package com.seocoo.worker.base.widget;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.seocoo.worker.R;

/***
 *评价星数view
 */
public class BaseStarOrangeView extends ConstraintLayout implements View.OnClickListener {


    private int position = -1;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private ImageView iv5;

    public BaseStarOrangeView(Context context) {
        super(context);
        initView(context);
    }

    public BaseStarOrangeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BaseStarOrangeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        View view = inflate(context, R.layout.base_star_orange_layout, this);
        iv1 = view.findViewById(R.id.iv1);
        iv2 = view.findViewById(R.id.iv2);
        iv3 = view.findViewById(R.id.iv3);
        iv4 = view.findViewById(R.id.iv4);
        iv5 = view.findViewById(R.id.iv5);

        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);
        iv4.setOnClickListener(this);
        iv5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == iv1) {
            iv1.setSelected(true);
            iv2.setSelected(false);
            iv3.setSelected(false);
            iv4.setSelected(false);
            iv5.setSelected(false);
            position = 1;
        } else if (v == iv2) {
            iv1.setSelected(true);
            iv2.setSelected(true);
            iv3.setSelected(false);
            iv4.setSelected(false);
            iv5.setSelected(false);
            position = 2;
        } else if (v == iv3) {
            iv1.setSelected(true);
            iv2.setSelected(true);
            iv3.setSelected(true);
            iv4.setSelected(false);
            iv5.setSelected(false);
            position = 3;
        } else if (v == iv4) {
            iv1.setSelected(true);
            iv2.setSelected(true);
            iv3.setSelected(true);
            iv4.setSelected(true);
            iv5.setSelected(false);
            position = 4;
        } else if (v == iv5) {
            iv1.setSelected(true);
            iv2.setSelected(true);
            iv3.setSelected(true);
            iv4.setSelected(true);
            iv5.setSelected(true);
            position = 5;
        }
    }

    /**
     * 当前评价为几星
     *
     * @return
     */
    public int getStar() {
        return position;
    }


    public void setShowNoEnabledPosition(int position) {
        this.position = position;
        iv1.setVisibility(GONE);
        iv2.setVisibility(GONE);
        iv3.setVisibility(GONE);
        iv4.setVisibility(GONE);
        iv5.setVisibility(GONE);

        if (position == 1) {
            iv1.setSelected(true);
            iv1.setVisibility(VISIBLE);
        }else if (position == 2) {
            iv1.setSelected(true);
            iv1.setVisibility(VISIBLE);
            iv2.setSelected(true);
            iv2.setVisibility(VISIBLE);

        }else if (position == 3) {
            iv1.setSelected(true);
            iv1.setVisibility(VISIBLE);
            iv2.setSelected(true);
            iv2.setVisibility(VISIBLE);
            iv3.setSelected(true);
            iv3.setVisibility(VISIBLE);
        }
        else if (position == 4) {
            iv1.setSelected(true);
            iv1.setVisibility(VISIBLE);
            iv2.setSelected(true);
            iv2.setVisibility(VISIBLE);
            iv3.setSelected(true);
            iv3.setVisibility(VISIBLE);
            iv4.setSelected(true);
            iv4.setVisibility(VISIBLE);
        } else if (position == 5) {
            iv1.setSelected(true);
            iv2.setSelected(true);
            iv3.setSelected(true);
            iv4.setSelected(true);
            iv5.setSelected(true);
            iv1.setVisibility(VISIBLE);
            iv2.setVisibility(VISIBLE);
            iv3.setVisibility(VISIBLE);
            iv4.setVisibility(VISIBLE);
            iv5.setVisibility(VISIBLE);
        }
    }
}
